#include <hd_oled.h>
#include <pd_gpio.h>
#include <hd_led.h>
#include <pd_spi_io.h>
#include <pd_spi_hd.h>
#include <hd_lcd.h>
#include <delay.h>
#include <sd_encode.h>
#include <hd_oled_protocol_i2c.h>
#include <hd_lcd_old.h>
#include <pd_i2c_hd.h>
#include <hd_flash.h>
#include "main.h"

#define OLED_PROTOCOL       2   // Select a protocol. SPI, 1; I2C, 2;
#define OLED_PROTOCOL_IMPL  2   // Select a implement. simulate, 1; hardware, 2;

uint8_t times = 0;
static OLED *oled;

void oled_instance();

extern void SystemClock_Config(void);

int main(void) {
    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

#ifdef OLED_SPI
    GPIO sckGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_5);  // Clock, SPIx_SCK
    GPIO sdaGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_7);  // MOSI,  SPIx_MOSI
    GPIO resGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_3);  // Reset, General IO, the chip reset pin is better
    GPIO dcGPIO  = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_1);  // Data or Command, General IO
    GPIO csGPIO  = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_2);  // Chip Select(Slave Select), General IO
    SPIGPIO spiGPIO = SPIGPIOBuilder.build(&csGPIO, 1, &sckGPIO, &sdaGPIO, NULL);

#ifdef OLED_SPI_SIMULATE
    SPI *spi = (SPI *) SPISimulateBuilder.build(&spiGPIO);
    spi->init(spi);
#elif OLED_SPI_HARDWARE
    SPI *spi = (SPI *) SPIHardwareBuilder.build(SPI1, &spiGPIO);
    spi->init(spi);
#endif
    OLEDProtocolSPI *oledProtocolObj = OLEDProtocolSPIBuilder.build(spi, &resGPIO, &dcGPIO, &csGPIO);

#elif OLED_I2C
    GPIO sckGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_10);  // I2Cx_SCL
    GPIO sdaGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_11);  // I2Cx_SDA
#ifdef OLED_I2C_SIMULATE
    I2CSimulate i2CObj = I2CSimulateBuilder.build(&sckGPIO, &sdaGPIO);
#elif OLED_I2C_HARDWARE
    I2CHardware i2CObj = I2CHardwareBuilder.build(I2C2, &sckGPIO, &sdaGPIO);
#endif
    I2C *i2c = (I2C *) &i2CObj;
    i2c->init(i2c);
    OLEDProtocolI2C *oledProtocolObj = OLEDProtocolI2CBuilder.build(i2c);
#endif

    oled = OLED.new(OLED_SIZE_16, OLED_MODE_POSITIVE, (OLEDProtocol *) oledProtocolObj);
    oled->init(oled);

    oled->clear(oled);
    oled->refresh(oled);

    // SPI hardware init
    GPIO sselGPIO[] = {GPIOBuilder.build(GPIOC, LL_GPIO_PIN_4)};
    GPIO sclkGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_5);
    GPIO misoGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_6);
    GPIO mosiGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_7);
    SPIGPIO spiGPIO = SPIGPIOBuilder.build(sselGPIO, 1, &sclkGPIO, &mosiGPIO, &misoGPIO);
    SPI *spi = (SPI *) SPIHardwareBuilder.build(SPI1, &spiGPIO);
    spi->init(spi);

    // LCD初始化
    HD_LCD_Init();

    HD_LCD_ShowChar(0, 0, ':');
    HD_LCD_ShowString(48, 0, (uint8_t *) "TFT LCD");

    uint64_t unicode = utf8_2_unicode((uint8_t *) "ʼ");
    HD_LCD_SetFontColor(RED);
    HD_LCD_ShowUnsignedNum(0, 16, unicode, 10);
    HD_LCD_SetFontColor(GREEN);
    HD_LCD_ShowSignedNum(88, 16, 123, 4);
    HD_LCD_SetFontColor(BLUE);
    HD_LCD_ShowSignedNum(128, 16, -123, 4);

    HD_LCD_SetFontColor(BRRED);
    HD_LCD_ShowHexNum(0, 32, unicode, 10);
    HD_LCD_SetFontColor(LIGHTBLUE);
    HD_LCD_ShowBinNum(0, 48, unicode, 20);

    HD_LCD_SetFontColor(BLACK);
    HD_LCD_SetFontBgColor(RED);
    float t = 0;
    while (1) {
         HD_LCD_ShowFloatNum(0, 64, t, 8);
        t += 0.11;
        delay_ms(100);

    }

}

void oled_instance() {
#if OLED_PROTOCOL == 1
    GPIO *sckGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_5); // Clock, SPIx_SCK
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_7); // MOSI,  SPIx_MOSI
    GPIO *resGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_3); // Reset, General IO, the chip reset pin is better
    GPIO *dcGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_1);  // Data or Command, General IO
    GPIO *csGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_2);  // Chip Select(Slave Select), General IO
    SPIGPIO *spiGPIO = SPIGPIOBuilder.build(&csGPIO, 1, sckGPIO, sdaGPIO, NULL);

#if OLED_PROTOCOL_IMPL == 1
    SPISimulate *spiObj = SPISimulateBuilder.build(spiGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    SPIHardware *spiObj = SPIHardwareBuilder.build(SPI1, spiGPIO);
#endif
    SPI *spi1 = (SPI *) spiObj;
    spi1->init(spi1);
    OLEDProtocolSPI *oledProtocolObj = OLEDProtocolSPIBuilder.build(spi1, resGPIO, dcGPIO, csGPIO);

#elif OLED_PROTOCOL == 2
    GPIO *sckGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_10);  // I2Cx_SCL
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_11);  // I2Cx_SDA
#if OLED_PROTOCOL_IMPL == 1
    I2CSimulate *i2CObj = I2CSimulateBuilder.build(sckGPIO, sdaGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    I2CHardware *i2CObj = I2CHardwareBuilder.build(I2C2, sckGPIO, sdaGPIO);
#endif
    I2C *i2c = (I2C *) i2CObj;
    i2c->init(i2c);
    OLEDProtocolI2C *oledProtocolObj = OLEDProtocolI2CBuilder.build(i2c);
#endif

    StorageFlash *storageFlash = StorageFlashBuilder.build();
    OLEDFont *oledFont = OLEDFontBuilder.build((Storage *) storageFlash, ENCODING_UTF8);
    oled = OLEDBuilder.build((OLEDProtocol *) oledProtocolObj, oledFont);
    oled->init(oled);
}
